<?php

register_shutdown_function(function() {
  $lastError = error_get_last();
  if (!is_null($lastError)) {
    $_SESSION['error'] = print_r($lastError, true);
  }
});

include 'config.inc.php';
include _ITSTOCK_LIB_DIR_.'save.inc.php';

itstock_session_start();

function itstock_delete_structure($data) {
  if (!itstock_structure($structure, $data['type'])) {
    $_SESSION['error'] = "Error: Can't find type: " . $data['type'];
  }

  $structure['data'][0]['id'] = $data['id'];

  if (itstock_delete($structure)) {
    $_SESSION['msg'] = 'Deleted Successfuly.';
  } else {
    $_SESSION['error'] = (array_key_exists($structure['errno'], $GLOBALS['DB_ERRORS'])? sprintf($GLOBALS['DB_ERRORS'][$structure['errno']],$structure['type']):$structure['msg']);
  }
  itstock_db_close_all();
  header('Location: '.$_SESSION['on_delete'][$data['type']]);
  exit();
}

if (isset($_GET["id"]) && isset($_GET["type"])) {
  if (!itstock_valid_login($_SESSION['user'])) {
    header("Location: index.php");
    exit();
  }
  itstock_delete_structure($_GET);
  exit();
}

header("Location: index.php");
?>
